<?php
/** 
 * Basic router controller
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */

/**
 * @namespace
 */
namespace X\Controller\Router;

use \X\Controller;
use \X\Controller\Event;
use \X\Controller\Response;

use \X\Controller\Front;
use \X\Controller\Handler;
use \X\DataStructure\String;

/**
 * simple export controler
 *
 */
class Relief implements Controller
{
    /**
     * prefix for handler classes
     *
     * @var string
     */
    protected $_HandlerSubNamespace = "Handlers\\";
    /**
     * handle input
     *
     * @param X\Controller\Event\EventInterface $Event
     * @param X\Controller\Event\ResponseInterface $tmpResponse
     * @return string|X\Controller\Event\Response
     */
    public function respond(Event $Event, Response $Response)
    {
        $AppNamespace = Front::ApplicationNamespace();

        // get sub namespace
        $SubNamespace = $Event->getStep();
        // default the namespace to Index
        if (empty($SubNamespace)) $SubNamespace = 'Index';

        // get the name of the handler for this data
        $HandlerName = $Event->getStep();
        // default handler to Index
        if (empty($HandlerName)) $HandlerName = 'Index';

        // then get the event handler name
        $CurrentEventHandler = $AppNamespace . $this->_HandlerSubNamespace .
                \X\DataStructure\String::toCapWords($SubNamespace) . '\\' .
                \X\DataStructure\String::toCapWords($HandlerName);

        // if a controller interface, run it that way
        if (\X\Loader::loadClass($CurrentEventHandler, Front::getRegistered('controller_path')))
        {
            if (\in_array('X\Controller\Handler\ReliefHandlerInterface',  \class_implements($CurrentEventHandler, false)))
            {
                // create handler instance

                $oEventHandler = new $sCurrentEventHandler();
                $Method = 'handle' . $this->_translateAction($Event->getMethod());
                switch ((string)$Action)
                {
                    case 'POST':
                        $tmpResponse = $oEventHandler->handleUpdate($Event->getData());
                        break;
                    case 'PUT':
                        $tmpResponse = $oEventHandler->handleNew($Event->getData());
                        break;
                    case 'DELETE':
                        $tmpResponse = $oEventHandler->handleDelete($Event->getStep());
                        break;
                    default: // GET
                        $tmpResponse = $oEventHandler->handleGet($Event->getStep(), $Event->getData());
                        break;
                }
                $Method();
            }
            else
            {
                throw new X\Controller\Handler\Exception("$CurrentEventHandler does not implemenet X\Controller\Handler\ReliefHandlerInterface");
            }
        }
        else
        {
            throw new \X\Controller\AbsentException("unable to route event $CurrentEventHandler");
        }

        if ($tmpResponse)
        {
            return $tmpResponse;
        }

        throw new \X\Controller\AbsentException('unable to route event ' . $CurrentEventHandler);
    }

}
